package com.xiaotu.spider.project.pageprocessor.nettv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 DouBanComListPageProcessor
 * @日期 2017年10月13日
 * @作者 王艳龙
 * @功能 豆瓣爬取类--TV短评
 */
@Component("DouBanNetTVPageProcessor")
@Scope("prototype")
public class DouBanNetTVPageProcessor extends AUrlPageProcessor
{
	
    private String fileComment; //豆瓣短评
    
    @Override
    protected String[] getStartRequestUrls()
    {
        List<String> linkList = new ArrayList<String>();
        	linkList.add("https://movie.douban.com/subject/26743546/comments?status=P&movieName="+KeywordEncode.encode("求婚大作战"));
        	linkList.add("https://movie.douban.com/subject/10756862/comments?status=P&movieName="+KeywordEncode.encode("转身说爱你"));
        	linkList.add("https://movie.douban.com/subject/26738092/comments?status=P&movieName="+KeywordEncode.encode("别那么骄傲"));
        	linkList.add("https://movie.douban.com/subject/26954013/comments?status=P&movieName="+KeywordEncode.encode("别那么骄傲2"));
        String[] urlArr = new String[linkList.size()];
        linkList.toArray(urlArr);
        return urlArr;

    }

    @Override
    public void initRunParams(Map<String, Object> map)
    {
        if (map.get("fileComment") == null)
            throw new SpiderException(
                    SpiderExceptionCode.PageProcessorParameterErrorException);
        fileComment = map.get("fileComment") + StringUtils.EMPTY;
    }

    @Override
    public String getPageDataType(Page page)
    {
        String url = page.getUrl().toString();
        if (url.contains("https://www.douban.com/search?cat=1002&q=")){
        	return Constants.PageDataType.SEARCH_LIST;
        	
        }else if (url.contains("https://www.douban.com/link2/")){
        	return Constants.PageDataType.TV_PRO_COMMENT;
        	
        }else if(url.contains("/comments")){
        	 return Constants.PageDataType.TV_COMMENT;
        	 
        }else if (url.contains("/reviews"))
            return Constants.PageDataType.TV_REVIEWS;
        return Constants.PageDataType.TV_PRO_COMMENT;
    }

    @Override
    public void process(Page page)
    {
        Map<String, Object> tvMap = page.getRequest().getExtras();
        String url = page.getUrl().toString();
        if(url.contains("movieName")){
        	String movieName = url.substring(url.indexOf("movieName=") + 10, url.length());
        	tvMap.put("movieName",KeywordEncode.decode(movieName));
        }
        switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.TV_COMMENT:
                this.commentProcess(page, tvMap);
                break;
            default:
                break;
                
                
                
        }
    }

    private void commentProcess(Page page, Map<String, Object> tvMap){
    	List<String> divList = page.getHtml()
				.xpath("//div[@class='comment-item']").all();
		if (divList == null || divList.isEmpty())
			throw new SpiderException(SpiderExceptionCode.DataInvalideException);
		for (String divStr : divList) {
			Html divObj = new Html(divStr);
			//电影名称
			String movieName = (String) tvMap.get("movieName");
			if (movieName == null || movieName.isEmpty())
				return;
			//评论人
			String comReviewer = divObj.xpath("//div[@class='avatar']/a/@title")
					.get();
			if (comReviewer == null || comReviewer.isEmpty())
				return;
			//评论时间
			String comTime = divObj.xpath("//span[@class='comment-time']/@title")
					.get();
			if (comTime == null || comTime.isEmpty())
				return;
			//评论内容
			String comContent = divObj.xpath("//div[@class='comment']/p/text()")
					.get();
			if (comContent == null || comContent.isEmpty())
				return;
			//评论权重
			StringBuffer buffer = new StringBuffer();
			String votes = divObj.xpath("//span[@class='comment-vote']/span[@class='votes']/text()").get();
			if (votes == null || votes.isEmpty())
				return;
			String show_login = divObj.xpath(
					"//span[@class='comment-vote']/a/text()").get();
			if (show_login == null || show_login.isEmpty())
				return;
			String comment_votes = buffer.append(votes)
					.append(SepratorUtil.SEP_SPACE_EN).append(show_login).toString();
			//评论等级	
			String comLevel="";
			List<String> comLevelClass = divObj.xpath(
					"//span[@class='comment-info']/span/@class").all();
			for(String cla: comLevelClass){
				if(cla.contains("allstar")){
					comLevel = cla.substring(cla.indexOf("allstar")+7, cla.indexOf("0 rating"));
				}
			}
			
			String line = StringUtils.join(new String[]{StringUtil.nullToString(page.getRequest().getExtra("type")),
					movieName, comReviewer,comTime,comContent, 
					comment_votes,comLevel}, SepratorUtil.SEP_PIPE);
	        System.out.println(line);
	        FileUtil.writeFile(fileComment, line + SepratorUtil.LINE_SEPRATOR);
	        
		}
		//无条数限制，爬取所有数据
		String current = page.getHtml()
				.xpath("//div[@class='center']/a[@class='next']/@href").get();
		if (current == null || current.isEmpty())
			return;
		if (StringUtils.isNotEmpty(current)){
			PageUtils.addTargetRequest(page,current,page.getRequest().getExtras());
		}
       
    }

    @Override
    protected String getSpiderType()
    {
        return "DouBanNetTV";
    }
}
